package com.example.demo.param.controller;

import com.example.demo.account.entity.SecurityUser;
import com.example.demo.account.entity.SysUserRole;
import com.example.demo.common.utils.Constant;
import com.example.demo.common.utils.SpringSecurityUtils;
import com.example.demo.information.entity.School;
import com.example.demo.information.entity.info.StudentInfo;
import com.example.demo.information.service.SchoolService;
import com.example.demo.param.entity.Param;
import com.example.demo.param.service.ParamService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
public class ParamController {

    @Autowired
    private SchoolService schoolService;
    @Autowired
    private ParamService paramService;

    @RequestMapping("/toParamPhoto")
    public String toParamPhoto(ModelMap map){
        SecurityUser currentUserDetails = SpringSecurityUtils.getCurrentUserDetails();
        SysUserRole currSysUserRole = currentUserDetails.getCurrSysUserRole();
        String xxStatus = Constant.XX_USER_ID.equals(currSysUserRole.getRole_id())?"true":"false";
        map.addAttribute("xxStatus",xxStatus);
        return "param/paramPhotoList";
    }

    @RequestMapping("/paramPhotoList")
    @ResponseBody
    public Map<String,Object> paramPhotoList(String searchVal,Integer page,Integer limit){
        Map<String ,Object> map = new HashMap<>();//用于传回页面参数
        Map<String,Object> paramMap = schoolService.getCurrPageSchoolList(searchVal,page,limit);
        int count = paramMap.containsKey("count")?(int) paramMap.get("count"):0;
        List<School> schoolList = paramMap.containsKey("schoolList")
                ? (List<School>) paramMap.get("schoolList") :new ArrayList<>();
        List<Map<String,Object>> list = paramService.loadSchoolPhotoParamJson(schoolList);//记录拼接页面的list
        map.put("data",list);
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        return map;
    }

    @RequestMapping("/toPhotoParamAdd")
    public String toPhotoParamAdd(ModelMap map){
        Map<String,String> tempSpidMap = new LinkedHashMap<>();
        for(Map.Entry<String,String> entry:Constant.photoParamSpidMap.entrySet()){
            String spidStr = entry.getValue();
            String[] split = spidStr.split("@@");
            spidStr = split[0]+"("+split[1]+")";
            tempSpidMap.put(entry.getKey(),spidStr);
        }
        map.put("spidList",tempSpidMap);
        map.put("bkColorList",Constant.photoParamBkColorMap);
        List<Param> paramList = paramService.findByPzsmAndXxxxIdNull(Constant.PARAM_PZSM_PHOTO);
        Param param = paramList!=null&&!paramList.isEmpty()?paramList.get(0):null;
        map.put("photoParam",param);
        map.put("photoParamSpid",param.getPzz());
        return "param/paramPhotoAdd";
    }

    @RequestMapping("/photoParamAdd")
    @ResponseBody
    public String photoParamAdd(String xxId,String paramId,String spid,String bkColor){
        //TODO 暂时隐去
        //(StringUtils.isNotBlank(paramId)){
            return paramService.savePhotoParamForXxIds(xxId,spid,bkColor);
        //}else{
        //    return paramService.savePhotoParamForParamIds(paramId,spid,bkColor);
        //}
    }

    @RequestMapping("/resetPhotoParam")
    @ResponseBody
    public String resetPhotoParam(String xxId){
        return paramService.savePhotoParamForXxIds(xxId,Constant.defaultPhotoParamSpid,Constant.defaultPhotoParamBkColor);
    }

    @RequestMapping("/toParamPhotoOutName")
    public String toParamPhotoOutName(ModelMap map){
        SecurityUser currentUserDetails = SpringSecurityUtils.getCurrentUserDetails();
        SysUserRole currSysUserRole = currentUserDetails.getCurrSysUserRole();
        String xxStatus = Constant.XX_USER_ID.equals(currSysUserRole.getRole_id())?"true":"false";
        map.addAttribute("xxStatus",xxStatus);
        return "param/paramPhotoNameList";
    }

    @RequestMapping("/paramPhotoNameList")
    @ResponseBody
    public Map<String,Object> paramPhotoNameList(String searchVal,Integer page,Integer limit){
        Map<String ,Object> map = new HashMap<>();//用于传回页面参数
        Map<String,Object> paramMap = schoolService.getCurrPageSchoolList(searchVal,page,limit);
        int count = paramMap.containsKey("count")?(int) paramMap.get("count"):0;
        List<School> schoolList = paramMap.containsKey("schoolList")
                ? (List<School>) paramMap.get("schoolList") :new ArrayList<>();
        List<Map<String,Object>> list = paramService.loadSchoolPhotoNameParamJson(schoolList);//记录拼接页面的list
        map.put("data",list);
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        return map;
    }

    @RequestMapping("/toPhotoNameParamAdd")
    public String toPhotoNameParamAdd(ModelMap map){
        List<Param> photoNameParamList = paramService.findByPzsmAndXxxxIdNull(Constant.PARAM_PZSM_PHOTONAME);
        Param photoNameParam = photoNameParamList!=null&&!photoNameParamList.isEmpty()?photoNameParamList.get(0):null;
        map.put("photoNameParam",photoNameParam);
        return "param/paramPhotoNameAdd";
    }

    @RequestMapping("/photoNameParamAdd")
    @ResponseBody
    public String photoNameParamAdd(String xxId,String photoName){
        return paramService.savePhotoNameParamForXxIds(xxId,photoName);
    }

    @RequestMapping("/resetPhotoNameParam")
    @ResponseBody
    public String resetPhotoNameParam(String xxId){
        return paramService.savePhotoNameParamForXxIds(xxId,Constant.defaultPhotoNameParam);
    }

    @RequestMapping("/toSelectPhotoNameParam")
    public String toSelectPhotoNameParam(){
        return "param/selectPhotoNameParam";
    }

    @RequestMapping("/getPhotoNameParamTransfer")
    @ResponseBody
    public Map<String,Object> getPhotoNameParamTransfer(){
        Map<String,Object> returnMap = new LinkedHashMap<>();
        Map<String,String> paramMap = new LinkedHashMap<>();
        paramMap.putAll(Constant.photoNameParamFromStuMap);
        paramMap.putAll(Constant.photoNameParamFromStuMapOther);
        paramMap.putAll(Constant.photoNameParamFromDateToStrMap);
        int count = 0;
        for(Map.Entry<String,String> entry:paramMap.entrySet()){
            Map<String,String> tempMap = new HashMap<>();
            tempMap.put("value",entry.getKey());
            tempMap.put("title",entry.getValue());
            returnMap.put(String.valueOf(count),tempMap);
            count++;
        }
        return returnMap;
    }

    @RequestMapping("/getNameParamDemo")
    @ResponseBody
    public Map<String,Object> getNameParamDemo(@RequestParam("nameParam")String param){
        Map<String ,Object> map = new HashMap<>();//用于传回页面参数
        StudentInfo studentInfo = new StudentInfo(Constant.testStudent);
        try{
            Map<String, String> photoNameByNameParam = paramService.getPhotoNameByNameParam(param, studentInfo);
            map.put("nameParamStr",photoNameByNameParam.get("nameParamStr"));
            map.put("nameParamPre",photoNameByNameParam.get("nameParamPre"));
            map.put("msg","success");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("msg","error");
        }
        return map;
    }
}
